package com.hod.roommap.client.model;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ListBox;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.VType;
import com.hod.roommap.client.HOD;

public class RoomFormPanel extends HODForm {
	
	protected final NumberField txtIdRoom  = new NumberField ("idRoom","idRoom",150); 					 
	protected final TextField txtnumber  = new TextField ("number","number",150); 																		 
	protected final NumberField txtfloor  = new NumberField ("floor","floor",150); 																		 
	protected final ListBox txtIdRoomType  = new ListBox (); 														 
	//protected final CheckBox txtactive = new  CheckBox ("active"); 																		 
	protected final CheckBox txtsharedBath = new  CheckBox ("sharedBath");
	protected final ListBox txtState = new ListBox();

	public RoomFormPanel(GearsDataStore ds) {
		super(ds);
		// TODO Auto-generated constructor stub
		add(createFieldSet());        
        createButtons();
	}

	@Override
	protected void createButtons() {
		// TODO Auto-generated method stub
		ButtonListenerAdapter savelistener = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {
            	try {
            		Room o = null;
            		if (txtIdRoom.getText()==""){
            			o = new Room();
            			//o.setIdRoomType(-1);
            			//RoomTypeRecordset.increaseRoomTypeCapacity(Integer.valueOf(txtIdRoomType.getValue(txtIdRoomType.getSelectedIndex()))); //incremento el numero de habitacion para ese tipo de habitacion al dar de alta
            		 }else{
            			o = (Room) ds.findById(new Integer(txtIdRoom.getText()));
            		 }                		
                    o.setState(txtState.getItemText(txtState.getSelectedIndex()));
                    o.setNumber(txtnumber.getText());
                    o.setFloor(Integer.valueOf(txtfloor.getText()));
                    //o.setActive(txtactive.getValue());
                    o.setSharedBath(txtsharedBath.getValue());
         			 /*if (!o.getIdRoomType().equals(Integer.valueOf(txtIdRoomType.getValue(txtIdRoomType.getSelectedIndex())))){
         				RoomTypeRecordset.decreaseRoomTypeCapacity(o.getIdRoomType()); 
         				RoomTypeRecordset.increaseRoomTypeCapacity(Integer.valueOf(txtIdRoomType.getValue(txtIdRoomType.getSelectedIndex()))); //incremento el numero de habitacion para ese tipo de habitacion al dar de alta
         			 }*/
                    o.setIdRoomType(Integer.valueOf(txtIdRoomType.getValue(txtIdRoomType.getSelectedIndex())));
                    ds.save(o);
                    HOD.reload();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						Window.alert(e1.getMessage());
					}
            }
        };
        ButtonListenerAdapter deletelistener = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {
            	try {
            		Room room = (Room) ds.findById(new Integer(txtIdRoom.getText()));
            		RoomTypeRecordset.decreaseRoomTypeCapacity(room.getIdRoomType());
            		ds.delete(room);
            		HOD.reload();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						Window.alert(e1.getMessage());
					}
            }
        };
        ButtonListenerAdapter createlistener = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {
            	try {
            		getForm().reset();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						Window.alert(e1.getMessage());
					}
            }
        };
        	           
        
        Button save = new Button("Save", savelistener);
        Button delete = new Button("Delete", deletelistener);
		Button newRecord = new Button("New", createlistener);
        Button cancel = new Button("Cancel");

        addButton(save);
        addButton(delete);
        addButton(cancel);
        addButton(newRecord);

	}

	@Override
	protected FieldSet createFieldSet() {
		// TODO Auto-generated method stub
		txtState.addItem("FREE");
        txtState.addItem("OCCUPIED");
        txtState.addItem("RESERVED");
        
        
		for(RoomType rt: RoomTypeRecordset.getRoomTypes()){
			txtIdRoomType.addItem(rt.getName(),String.valueOf(rt.getIdRoomType()));
		}
        FieldSet fieldSet = new FieldSet();
        fieldSet.setLabelWidth(80);
        fieldSet.setTitle("Rooms");
        fieldSet.setAutoHeight(true);
        fieldSet.setBorder(false);

        //the field names msut match the data field values from the Store
        fieldSet.add(txtIdRoom);
        fieldSet.add(txtfloor);            
        fieldSet.add(txtnumber);
        fieldSet.add(new Label("Estado"));
        fieldSet.add(txtState);
        fieldSet.add(new HTML("<BR/>"));
        fieldSet.add(new Label("Tipo de habitacion"));
        fieldSet.add(txtIdRoomType);
        //fieldSet.add(txtactive);    
        fieldSet.add(txtsharedBath);
                
        txtIdRoom.setDecimalPrecision(0);
        txtIdRoom.setVisible(false);
        txtIdRoom.setHideLabel(true);
        
        txtfloor.setDecimalPrecision(0);
        txtfloor.setAllowBlank(false);
        txtfloor.setAllowDecimals(false);
        txtfloor.setAllowNegative(false);
        
        txtnumber.setAllowBlank(false);
        txtnumber.setVtype(VType.ALPHANUM);

        return fieldSet;
	}

	@Override
	public void updateDataControls(Record record) {
		getForm().loadRecord(record);     
		txtsharedBath.setValue(record.getAsBoolean("sharedBath"));
		for (int i=0;i<txtState.getItemCount();i++)
		    if (record.getAsString("state").equals(txtState.getItemText(i))){
		    	txtState.setItemSelected(i, true);
		    	break;
		    }
		for (int i=0;i<txtIdRoomType.getItemCount();i++)
		    if (Integer.valueOf(txtIdRoomType.getValue(i)).equals(record.getAsInteger("idRoomType")))
		       txtIdRoomType.setItemSelected(i, true);

	}

	@Override
	public void setObject(GearsDataObject object) {
		// TODO Auto-generated method stub
		Room room = (Room) object;
		try {
			this.txtIdRoom.setValue(room.getIdRoom());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			this.txtIdRoom.setValue("");
		}
		try {
			this.txtnumber.setValue(room.getNumber());
		} catch (Exception e) {			
			// TODO Auto-generated catch block
			this.txtnumber.setValue("");
		}
		try {
			this.txtsharedBath.setValue(room.getSharedBath());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			this.txtsharedBath.setValue(false);
		}
		try {
			this.txtfloor.setValue(room.getFloor());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			this.txtfloor.setValue(0);
		}
		for (int i=0;i<txtIdRoomType.getItemCount();i++)
		    if (Integer.valueOf(txtIdRoomType.getValue(i)).equals(room.getIdRoomType())){
		       txtIdRoomType.setItemSelected(i, true);
		       break;
		    }
		for (int i=0;i<txtState.getItemCount();i++)
		    if (room.getState().equals(txtState.getItemText(i))){
		    	txtState.setItemSelected(i, true);
		    	break;
		    }	
	}

}
